package com.diaohw.platform.framework.db.utils;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.diaohw.platform.common.enums.CodeMsgEnum;
import com.diaohw.platform.framework.db.obj.BaseModel;

import java.util.List;

/**
 * 数据校验SQL封装
 */
public class DataCheckUtils {

    /**
     * 等于校验
     *
     * @param column 校验字段
     * @param value  校验字段值
     * @param mapper Mapper
     */
    public static <T, R> void eq(SFunction<T, R> column, R value, BaseMapper<T> mapper) {

        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(column, value);
        Long count = mapper.selectCount(wrapper);
        throwException(count, value);
    }

    /**
     * 等于校验
     *
     * @param prerequisiteFunction 前提条件字段
     * @param prerequisiteValue    前提条件值
     * @param column               校验字段
     * @param value                校验字段值
     * @param mapper               Mapper
     */
    public static <T> void eq(SFunction<T, ?> prerequisiteFunction, Object prerequisiteValue,
                              SFunction<T, ?> column, Object value,
                              BaseMapper<T> mapper) {

        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(column, value);
        wrapper.eq(ObjUtil.isNotNull(prerequisiteValue), prerequisiteFunction, prerequisiteValue);
        Long count = mapper.selectCount(wrapper);
        throwException(count, value);
    }


    /**
     * 等于校验
     *
     * @param column 校验字段
     * @param id     ID值
     * @param value  校验字段值
     * @param mapper Mapper
     */
    public static <T extends BaseModel<T>, R> void eq(SFunction<T, R> column,
                                                      String id, R value,
                                                      BaseMapper<T> mapper) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(column, value);
        wrapper.ne(BaseModel::getId, id);
        Long count = mapper.selectCount(wrapper);
        throwException(count, value);
    }

    /**
     * 等于校验
     *
     * @param prerequisiteFunction 前提条件字段
     * @param prerequisiteValue    前提条件值
     * @param column               校验字段
     * @param value                校验字段值
     * @param mapper               Mapper
     */
    public static <T extends BaseModel<T>> void eq(SFunction<T, ?> prerequisiteFunction, Object prerequisiteValue,
                                                   SFunction<T, ?> column, Object value, String id,
                                                   BaseMapper<T> mapper) {

        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(column, value);
        wrapper.ne(BaseModel::getId, id);
        wrapper.eq(ObjUtil.isNotNull(prerequisiteValue), prerequisiteFunction, prerequisiteValue);
        Long count = mapper.selectCount(wrapper);
        throwException(count, value);

    }

    /**
     * in校验
     *
     * @param column 校验字段
     * @param value  校验字段值
     * @param mapper Mapper
     */
    public static <T, R> void in(SFunction<T, R> column,
                                 List<R> value,
                                 BaseMapper<T> mapper) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(column, value);
        Long count = mapper.selectCount(wrapper);
        throwException(count, "");

    }

    /**
     * in校验
     *
     * @param column 校验字段
     * @param ids    ID值
     * @param value  校验字段值
     * @param mapper Mapper
     */
    public static <T extends BaseModel<T>, R> void in(SFunction<T, R> column,
                                                      List<String> ids, List<R> value,
                                                      BaseMapper<T> mapper) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(column, value);
        wrapper.notIn(BaseModel::getId, ids);
        Long count = mapper.selectCount(wrapper);
        throwException(count, "");
    }


    public static void throwException(Long count, Object value) {
        if (count > 0) {
            throw CodeMsgEnum.DATA.build(String.format("参数值[%s]重复", value));
        }
    }
}
